package com.psyhotline.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.psyhotline.constant.Constant;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.*;
import org.apache.hc.client5.http.config.*;
import org.apache.hc.client5.http.impl.classic.*;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.*;
import org.apache.hc.core5.util.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

public class Http {

    public static JSONObject get(String url, String paramType, String params, String access){
        JSONObject testRes = new JSONObject();

        String realUrl = Constant.getBaseUrl() + url;

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(realUrl);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Timeout.ofDays(35000)).setConnectionRequestTimeout(Timeout.ofDays(35000)).build();
        httpGet.setConfig(requestConfig);

        if(Objects.equals(paramType, "")){
            paramType = "application/json";
        }

        httpGet.setHeader("Content-type", paramType);
        httpGet.setHeader("DataEncoding", "UTF-8");

        CloseableHttpResponse httpResponse = null;
        try {
            if(Objects.equals(access, "admin")) {
                String cookie = Cookie.getCookieLogin_admin();
                httpGet.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "counselor")) {
                String cookie = Cookie.getCookieLogin_counselor();
                httpGet.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "supervisor")) {
                String cookie = Cookie.getCookieLogin_supervisor();
                httpGet.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "user")) {
                String cookie = Cookie.getCookieLogin_user();
                httpGet.setHeader("Cookie", cookie);
            }

            if(!Objects.equals(params, "")){
                httpGet.setEntity(new StringEntity(params));
            }
            httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            String stringResult = EntityUtils.toString(entity);

            testRes = JSON.parseObject(stringResult);
        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return testRes;
    }

    public static JSONObject post(String url, String paramType, String params, String access){
        JSONObject testRes = new JSONObject();

        String realUrl = Constant.getBaseUrl() + url;

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(realUrl);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Timeout.ofDays(35000)).setConnectionRequestTimeout(Timeout.ofDays(35000)).build();
        httpPost.setConfig(requestConfig);

        if(Objects.equals(paramType, "")){
            paramType = "application/json";
        }

        httpPost.setHeader("Content-type", paramType);
        httpPost.setHeader("DataEncoding", "UTF-8");

        CloseableHttpResponse httpResponse = null;
        try {
            if(Objects.equals(access, "admin")) {
                String cookie = Cookie.getCookieLogin_admin();
                httpPost.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "counselor")) {
                String cookie = Cookie.getCookieLogin_counselor();
                httpPost.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "supervisor")) {
                String cookie = Cookie.getCookieLogin_supervisor();
                httpPost.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "user")) {
                String cookie = Cookie.getCookieLogin_user();
                httpPost.setHeader("Cookie", cookie);
            }

            if(!Objects.equals(params, "")){
                httpPost.setEntity(new StringEntity(params));
            }
            httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            String stringResult = EntityUtils.toString(entity);
            testRes = JSON.parseObject(stringResult);
        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return testRes;
    }

    public static JSONObject put(String url, String paramType, String params, String access){
        JSONObject testRes = new JSONObject();

        String realUrl = Constant.getBaseUrl() + url;

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(realUrl);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Timeout.ofDays(35000)).setConnectionRequestTimeout(Timeout.ofDays(35000)).build();
        httpPut.setConfig(requestConfig);

        if(Objects.equals(paramType, "")){
            paramType = "application/json";
        }

        httpPut.setHeader("Content-type", paramType);
        httpPut.setHeader("DataEncoding", "UTF-8");

        CloseableHttpResponse httpResponse = null;
        try {
            if(Objects.equals(access, "admin")) {
                String cookie = Cookie.getCookieLogin_admin();
                httpPut.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "counselor")) {
                String cookie = Cookie.getCookieLogin_counselor();
                httpPut.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "supervisor")) {
                String cookie = Cookie.getCookieLogin_supervisor();
                httpPut.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "user")) {
                String cookie = Cookie.getCookieLogin_user();
                httpPut.setHeader("Cookie", cookie);
            }


            if(!Objects.equals(params, "")){
                httpPut.setEntity(new StringEntity(params));
            }
            httpResponse = httpClient.execute(httpPut);
            HttpEntity entity = httpResponse.getEntity();
            String stringResult = EntityUtils.toString(entity);
            testRes = JSON.parseObject(stringResult);
        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return testRes;
    }

    public static JSONObject delete(String url, String paramType, String params, String access){
        JSONObject testRes = new JSONObject();

        String realUrl = Constant.getBaseUrl() + url;

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpDelete httpDelete = new HttpDelete(realUrl);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Timeout.ofDays(35000)).setConnectionRequestTimeout(Timeout.ofDays(35000)).build();
        httpDelete.setConfig(requestConfig);

        if(Objects.equals(paramType, "")){
            paramType = "application/json";
        }

        httpDelete.setHeader("Content-type", paramType);
        httpDelete.setHeader("DataEncoding", "UTF-8");

        CloseableHttpResponse httpResponse = null;
        try {
            if(Objects.equals(access, "admin")) {
                String cookie = Cookie.getCookieLogin_admin();
                httpDelete.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "counselor")) {
                String cookie = Cookie.getCookieLogin_counselor();
                httpDelete.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "supervisor")) {
                String cookie = Cookie.getCookieLogin_supervisor();
                httpDelete.setHeader("Cookie", cookie);
            }
            else if(Objects.equals(access, "user")) {
                String cookie = Cookie.getCookieLogin_user();
                httpDelete.setHeader("Cookie", cookie);
            }

            if(!Objects.equals(params, "")){
                httpDelete.setEntity(new StringEntity(params));
            }
            httpResponse = httpClient.execute(httpDelete);
            HttpEntity entity = httpResponse.getEntity();
            String stringResult = EntityUtils.toString(entity);
            testRes = JSON.parseObject(stringResult);
        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return testRes;
    }
}
